"""
去队列中获取任务,执行并写入到结果队列
"""
import json

import redis
import hashlib

REDIS_POOL = redis.ConnectionPool(host="127.0.0.1",port=6379, password="",encoding="utf-8",max_connections=1000)
TASK_QUEUE = "spider_task_list"
RESULT_QUEUE = "spider_result_dict"


def get_task():
    conn = redis.Redis(connection_pool=REDIS_POOL)
    data = conn.brpop(TASK_QUEUE, timeout=10)
    if not data:
        return None
    return json.loads(data[1].decode("utf-8"))

def set_result(tid, value):
    conn = redis.Redis(connection_pool=REDIS_POOL)
    conn.hset(RESULT_QUEUE,tid, value)

def run():
    while True:
        # 1. 获取任务
        task_dict = get_task() # 字符串
        print("task_dict",task_dict)
        if not task_dict:
            continue
        # {"tid": tid, "data": ordered_string}
        # 2.执行耗时操作
        ordered_string = task_dict["data"]
        encrypt_string = ordered_string + "56022qdasdww22fef2921swplax"
        obj = hashlib.md5(encrypt_string.encode('utf-8'))
        sign = obj.hexdigest()
        # 3. 写入到结果队列(redis的hash)
        tid = task_dict["tid"]
        set_result(tid, sign)

if __name__ == "__main__":
    run()